import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
from sklearn.model_selection import cross_val_score, StratifiedKFold


#读取数据和填充缺失值
data = pd.read_csv('../数据集/原数据.csv')
data=data.fillna(0)

#选择的特征
Features=['月就诊天数_MAX', '月就诊天数_AVG', '月就诊次数_MAX', '月就诊次数_AVG', '月统筹金额_MAX',
       '月统筹金额_AVG', '月药品金额_MAX', '月药品金额_AVG', '顺序号_NN', '个人账户金额_SUM',
       '统筹支付金额_SUM', 'ALL_SUM', '可用账户报销金额_SUM', '药品费发生金额_SUM', '药品费申报金额_SUM',
       '起付标准以上自负比例金额_SUM', '医疗救助个人按比例负担金额_SUM', '非账户支付金额_SUM', '本次审批金额_SUM',
       '医疗救助医院申请_SUM']
x=data[Features]
y=data["RES"]


# 将特征列和目标列分开
y = data['RES']  # 目标列

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)

# 创建决策树模型
clf = DecisionTreeClassifier()

# 在训练集上训练模型
clf.fit(X_train, y_train)

# 在测试集上进行预测
y_pred = clf.predict(X_test)

# 模型评估
accuracy = accuracy_score(y_test, y_pred)
print("准确率：", accuracy)

# 打印分类报告
print("分类报告：")
print(classification_report(y_test, y_pred))

# 打印混淆矩阵
print("混淆矩阵：")
print(confusion_matrix(y_test, y_pred))


# 创建决策树模型
clf = DecisionTreeClassifier()

# 定义交叉验证策略
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)

# 进行交叉验证
cv_scores = cross_val_score(clf, x, y, cv=cv, scoring='accuracy')

# 输出交叉验证的准确率
print("交叉验证准确率：", cv_scores)
print("平均准确率：", cv_scores.mean())